﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using BPMS.Utility.Web;
using System.Web;

namespace BPMS.Utility.Logging
{
    public class LogHelper
    {
        private static readonly ILog DbInfo;
        private static readonly ILog DbError;
        private static readonly ILog EmailLogger;
        private static readonly ILog DbLogger;

        static LogHelper()
        {
            log4net.Config.XmlConfigurator.Configure();
            DbInfo = LogManager.GetLogger("DbInfo");
            DbError = LogManager.GetLogger("DbError");
            EmailLogger = LogManager.GetLogger("EmailLogger");
            DbLogger = LogManager.GetLogger("DbLogger");
        }

        public static void ErrorToDb(Exception ex, EventCategory category = EventCategory.Default)
        {
            DbLogger.Error(GetLogEntry(EventLevel.ERROR, category, ex.ToString()));
        }

        public static void InfoToDb(string info, EventCategory category = EventCategory.Default)
        {
            DbLogger.Info(GetLogEntry(EventLevel.INFO, category, info));
        }

        public static void DbExcuteError(string commandText, Exception exception)
        {
            DbError.Error(new DbContextLogEntry() { CommandText = commandText, ExceptionInfo = exception.ToString() });
        }

        public static void DbExcuteRecord(string commandText, string parameters)
        {
            DbInfo.Info(new DbContextLogEntry() { CommandText = commandText, ParameterInfo = parameters });
        }

        public static void ErrorToEmail(Exception ex, EventCategory category = EventCategory.Default)
        {
            EmailLogger.Error(GetLogEntry(EventLevel.ERROR, category, ex.ToString()));
        }

        public static void InfoToEmail(string info, EventCategory category = EventCategory.Default)
        {
            EmailLogger.Info(GetLogEntry(EventLevel.INFO, category, info));
        }

        private static LogEntry GetLogEntry(EventLevel level, EventCategory category, string message)
        {
            StackTrace stackTrace = new StackTrace(true);
            MethodBase method = stackTrace.GetFrame(2).GetMethod();
            string methodName = (method == null ? "unknown" : method.Name);
            return new LogEntry()
            {
                AppDomainName = AppDomain.CurrentDomain.FriendlyName,
                ThreadName = Thread.CurrentThread.Name,
                ManchineName = Environment.MachineName,
                UserName = new FormsAuthenticationService().GetAuthenticatedNameByTicket(),
                MethodName = methodName,
                EventLevel = (byte)level,
                EventCategory = (byte)category,
                MessageInfo = message,
                CreateTime = DateTime.Now,
                Browser = ClientUtility.GetClientBrowser(),
                IP = ClientUtility.GetClientIP()
            };
        }
    }
}
